<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_tagihan_sewa extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_tagihan_sewa_model', 'Tagihan');
		//$this->load->model('pro_dt_tagihan_sewa_model', 'Detail');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Data Tagihan";
		$date = date('d-M-Y');
		$periode = 'Januari-2014';

		$t1 = explode('-', $date);
		$t2 = explode('-', $periode);

		viewByTemplate('pro_tagihan_sewa/index', $data, 'grid');
	}
	 
	function getdata()
	{
		$this->secure_session();
		echo $this->Tagihan->GetJSON();
	}

	function dtl_sample2_data()
	{
		// Ajax Response For Datagrid
		echo $this->Detail->GetJSON(post('no_detail_tagihan'));
	}

	function create()
	{
		// Display Form Create
		$data['title'] = "Tagihan Baru";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";
		$query = $this->db->query("SELECT no_tagihan_sewa FROM (SELECT * FROM pro_tagihan_sewa ORDER BY no_tagihan_sewa DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['no_tagihan_sewa'];
		else
			$MaxID = "TS0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['no_tagihan_sewa'] = "TS" . sprintf("%07s",$NextID);

		$query1 = $this->db->query("SELECT id_tagihan_sewa FROM (SELECT * FROM pro_tagihan_sewa ORDER BY id_tagihan_sewa DESC) WHERE rownum <=1");
	 				if ($query1->row_array()) {
	 					$getquery1 = $query1->row_array();
	 					$rowcount1 = $getquery1['id_tagihan_sewa'];
	 					$autonum1 = $rowcount1 + 1;
	 				} else {
					$autonum1 = 1;
				}
		$data['id_tagihan_sewa_auto'] = $autonum1;
		viewByTemplate('pro_tagihan_sewa/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Tagihan";
		$data['data'] = $this->Tagihan->GetSingleView(get('no_tagihan_sewa'));
		$no_tagihan_sewa = get('no_tagihan_sewa');
		
		$query = $this->db->query("SELECT tanggal_pembayaran FROM pro_tagihan_sewa WHERE no_tagihan_sewa = '".$no_tagihan_sewa."'");
        if ($query->row_array()) {
        $getquery = $query->row_array();
        $key1 = $getquery['tanggal_pembayaran'];
        }
		$key = explode("-" , $key1);

		$data['bulan'] = $key[0];
		$data['tahun'] = $key[1];
		$data['no_tagihan_sewa'] = $no_tagihan_sewa; 
		// Define Combo Data

		$data['mode'] = "edit";

		$data['id_tagihan_sewa_edit'] = get('id_tagihan_sewa');
		$data['no_tenant_edit'] = get('no_tenant');
		$data['id_lokasi_edit'] = get('id_lokasi');
		//var_dump($data['id_lokasi_edit']);
		viewByTemplate('pro_tagihan_sewa/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Tagihan";
		$data['data'] = $this->Tagihan->GetSingleView(get('no_tagihan_sewa'));
		
		$data['mode'] = "view";
		viewByTemplate('pro_tagihan_sewa/form', $data, 'form');
	}

	/*function edit_status()
	{
		// Display Form View
		$data['title'] = "Edit Status Bayar";
		$no_tagihan_sewa = get('no_tagihan_sewa');
		//$t = substr($no_tagihan_sewa, 1,10);
		$data['data'] = $this->Tagihan->dataEdit(get('no_tagihan_sewa'));
		$query = $this->db->query("SELECT tanggal_pembayaran FROM pro_tagihan_sewa WHERE no_tagihan_sewa = '".$no_tagihan_sewa."'");
        if ($query->row_array()) {
        $getquery = $query->row_array();
        $key1 = $getquery['tanggal_pembayaran'];
        }
		$key = explode("-" , $key1);
		var_dump($key1);

		$data['bulan'] = $key[0];
		$data['tahun'] = $key[1];
		$data['no_tagihan_sewa'] = $no_tagihan_sewa; 
		$data['mode'] = "edit_status";
		viewByTemplate('pro_tagihan_sewa/form_edit_status', $data, 'form');
	}*/

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("no_tagihan_sewa") == '') {
			$error->Add("no_tagihan_sewa", "No Tagihan Sewa", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM pro_tagihan_sewa WHERE no_tagihan_sewa = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("no_tagihan_sewa"))))."'")) > 0) {
			$error->Add("no_tagihan_sewa", "No Tagihan Sewa", $tpl_duplicate);
		}else if (post("nama_tenant") == '') {
			$error->Add("nama_tenant", "Nama Tenant", $tpl_empty);
		}else if (post("no_toko") == '') {
			$error->Add("no_toko", "No Toko", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$set_bulan_dari = $this->input->post('set_bulan_dari');
		$set_bulan_sampai = $this->input->post('set_bulan_sampai');
		$set_tahun_dari = $this->input->post('set_tahun_dari');
		$set_tahun_sampai = $this->input->post('set_tahun_dari');
		$set_status_bayar = $this->input->post('set_status_bayar');

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data = array(	'id_tagihan_sewa' => $this->input->post('id_tagihan_sewa'),
								'no_tagihan_sewa' => $this->input->post('no_tagihan_sewa'),
								'nama_tenant' => $this->input->post('nama_tenant'),
								'no_toko' => $this->input->post('no_toko'),
								'jenis_usaha' => $this->input->post('jenis_usaha'),
								'lokasi_tempat' => $this->input->post('lokasi_tempat'),
								'harga_sewa' => $this->input->post('harga_sewa'),
								'tanggal_bayar' => $this->input->post('tanggal_bayar'),
								'no_bukti_pembayaran' => $this->input->post('no_bukti_pembayaran'),
								'tanggal_pembayaran' => $this->input->post('bulan')."-".$this->input->post('tahun'),
								'status_bayar' => $this->input->post('status_bayar'),
								'id_bulan' => $this->input->post('bulan'),
								'no_tenant' => $this->input->post('no_tenant'),
								'id_lokasi' => $this->input->post('id_lokasi'),
							);

							$data['tanggal_bayar']	= ($data['tanggal_bayar'] == '' ? NULL : date_format3($data['tanggal_bayar'], 'YYYY-MM-DD'));	
				
				if (post("mode") == "create")
				{
					$this->Tagihan->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->Tagihan->Update($data, array('no_tagihan_sewa'	=> $data['no_tagihan_sewa']));
					if($this->input->post('mode_status')==1){
					$query = $this->db->query(" UPDATE pro_tagihan_sewa SET status_bayar='Sudah Bayar' 
												WHERE   (SUBSTR(tanggal_pembayaran, 4, INSTR(tanggal_pembayaran, '-')+2) = '$set_tahun_dari' AND SUBSTR(tanggal_pembayaran, 0, INSTR(tanggal_pembayaran, '-')-1) >='$set_bulan_dari') AND
			 											(SUBSTR(tanggal_pembayaran, 4, INSTR(tanggal_pembayaran, '-')+2) = '$set_tahun_sampai' AND SUBSTR(tanggal_pembayaran, 0, INSTR(tanggal_pembayaran, '-')-1) <='$set_bulan_sampai') AND 
														no_toko = '".$this->input->post('no_toko')."' ");

					}
					
					$ls_response = "Data Tersimpan [edited]";
				}
			}

		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}




	function ajax_delete()
	{
		$ls_result	= "";
		$pesan = "Data Terhapus";
		$no_tagihan_sewa	= get('no_tagihan_sewa');
		
		if ($no_tagihan_sewa != "")
		{
			// Delete Single
			$arr_rule	= array('no_tagihan_sewa' => $no_tagihan_sewa);
			$ls_result = $this->Tagihan->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('no_tagihan_sewa' => $row['no_tagihan_sewa']);
				$ls_result .= $this->Tagihan->DeleteSingle($arr_rule);
			}
		}
		
		echo $pesan;
	}
}
?>